package com.android.component;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.utils.LogUtil;

public class TimerTest extends Activity implements OnClickListener {

    TextView tvlog;

    class MyTimerTask extends TimerTask {
        String type = "";
        int times = 0;

        public MyTimerTask(String type) {
            this.type = type;
        }

        @Override
        public void run() {
            LogUtil.d("", type + " " + (times++));
            // tvlog.setText(tvlog.getText().toString() + "MyTimerTask不可以直接进行ui操作");
        }
    }

    // CountDownTimer(long millisInFuture, long countDownInterval)
    // 在millisInFuture倒计时的时长
    // 内每隔countDownInterval回调间隔

    CountDownTimer downtimer = new CountDownTimer(10000, 1000) {
        int times = 0;

        @Override
        public void onTick(long millisUntilFinished) {
            LogUtil.d("", "downtimer  " + (times++) + "," + millisUntilFinished);

        }

        @Override
        public void onFinish() {
            tvlog.append("\nCountDownTimer可以直接进行ui操作");
            LogUtil.e("", "downtimer  onFinish");
        }
    };

    Timer timer = new Timer();
    MyTimerTask timerTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timer);
        tvlog = (TextView) findViewById(R.id.tvlog);
        LogUtil.setTv(tvlog);

        findViewById(R.id.cancelTimer).setOnClickListener(this);
        findViewById(R.id.cancelTimerTask).setOnClickListener(this);
        findViewById(R.id.shedule_date).setOnClickListener(this);
        findViewById(R.id.shedule_date_cycle).setOnClickListener(this);
        findViewById(R.id.shedule_delay).setOnClickListener(this);
        findViewById(R.id.shedule_delay_cycle).setOnClickListener(this);
        findViewById(R.id.shedule_delay_cycle).setOnClickListener(this);
        findViewById(R.id.downTimerStart).setOnClickListener(this);
        findViewById(R.id.downTimerStop).setOnClickListener(this);
        findViewById(R.id.clear).setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cancelTimer) {
            timer.cancel();
            LogUtil.w("", "downtimer 可以重复start,但不管start多产次，cacel全部取消掉");
        } else if (v.getId() == R.id.cancelTimerTask) {
            if (timerTask != null) {
                timerTask.cancel();
                timerTask = null;
            }
        } else if (v.getId() == R.id.shedule_date) {
            timerTask = new MyTimerTask("date");
            timer.schedule(timerTask, new Date());
        } else if (v.getId() == R.id.shedule_date_cycle) {
            timerTask = new MyTimerTask("datecycle");
            timer.schedule(timerTask, new Date(), 1000);
        } else if (v.getId() == R.id.shedule_delay) {
            timerTask = new MyTimerTask("delay");
            timer.schedule(timerTask, 3000);
        } else if (v.getId() == R.id.shedule_delay_cycle) {
            timerTask = new MyTimerTask("delaycycle");
            timer.schedule(timerTask, 3000, 1000);
        } else if (v.getId() == R.id.downTimerStart) {
            downtimer.start();
            LogUtil.d("", "downtimer 可以重复start,但不管start多产次，cacel全部取消掉");
        } else if (v.getId() == R.id.downTimerStop) {
            downtimer.cancel();
        } else if (v.getId() == R.id.clear) {
            tvlog.setText("");
        }
    }
}
